<?php
class hschool_vod_hour{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*	取出一个课程信息
	*/
	public function get_one($id='') {
		$info = array();
		if($id){
			$sql = "SELECT * FROM ".DB::table('vod_hour')." WHERE id='".$id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}

	/*
	*	创建课程
	*/
	public function add() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		$v_id = isset($_REQUEST['v_id']) ? $_REQUEST['v_id']:'';
		if(!empty($is_submit)){
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			$data = array(
				'name' => $_REQUEST['name'],
				'title_page' => $_REQUEST['title_page'],
				'files_path' => implode(',',$_REQUEST['files']),				
				'create_id' => $_G['user_id'],
				'teacher_id' => trim($_REQUEST['teacher_id']),				
				'introduction' => $_REQUEST['introduction'],
				'duration' => $_REQUEST['duration'],
				'vod_url' => $_REQUEST['vod_url'],
				'dateline' => TIMESTAMP
			);
			if($id){
				$effect = DB::update('vod_hour',$data,array('id'=>$id));
			}else{
				$data['v_id'] = $v_id;
				$effect = DB::insert('vod_hour',$data,1);
			}
			if($effect){
				$return_data = array('v_id'=>$v_id);
				self::return_json(false,'操作成功',$return_data);
			}else{
				self::return_json();
			}
		}else{
			include template('admin/hschool/vod_hour_add');	
		}
	}

	/*
	*	编辑课程
	*/
	public function edit() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){

		}else{
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			$info = $this->get_one($id);
			$v_id=$info['v_id'];
			//DEBUG 取出文件库存储的资源文件
			$mul_student = strpos($info['files_path'],',');
			if($mul_student){
				$student_file_ids = explode(',',$info['files_path']);
			}else{
				$student_file_ids[] = $info['files_path'];
			}
			if($student_file_ids){
				$hschool_files = load_class('hschool','files');
				if($hschool_files && $student_file_ids){
					$more_student = '';
					$more_student = $hschool_files->get_more($student_file_ids);
				}
			}
			
			include template('admin/hschool/vod_hour_add');
		}
	}		

	/*
	*	删除课时
	*/
	public function del() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
			if(!empty($is_submit)){
			$course_hour_ids_array = array();
			$course_hour_ids_array = isset($_REQUEST['course_hour_ids']) ? $_REQUEST['course_hour_ids']:array();
			if($course_hour_ids_array){
				$update = array(
					'isdelete'=> 1
				);
				$where = ' id IN ('.dimplode($course_hour_ids_array).') ';
				$effects = DB::update('vod_hour',$update,$where);
			}
			//DEBUG 接收用户信息 end
			$this->return_json(false,'操作成功');
			echo json_encode($return_array);
		}
	}

	/*
	*	当前动作的首页
	*/
	public function index() {
		global $_G;
		$page_array = array();
		$sqlcount = $sql = $wheresql = $title_keyword = $pageNum = $numPerPage = '';
		$pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
		$numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 50;
		$pagestart = ($pageNum - 1) * $numPerPage;
		$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
		$vod_hour_name = isset($_REQUEST['vod_hour_name']) ? $_REQUEST['vod_hour_name'] : '';
		
		if($vod_hour_name){
			$wheresql .= " AND c.name LIKE '%".$vod_hour_name."%'";
		}
		
		if($id){
			$wheresql .= " AND c.v_id = '".$id."'";
		}
		$sqlcount = "SELECT count(*) from ".DB::table('vod_hour')." AS c WHERE c.isdelete=0 ".$wheresql;
		$pagetotal = DB::result_first($sqlcount);
		$sql = "SELECT c.* from ".DB::table('vod_hour')." AS c WHERE c.isdelete=0 ".$wheresql." ORDER BY c.id DESC LIMIT ".$pagestart.",".$numPerPage;
		$page_array = DB::fetch_all($sql);
		foreach($page_array AS $key => $value){
			//DEBUG 取出授课老师名称
			$page_array[$key]['teacher_name']=ext::user_realname($value['teacher_id']);
			//DEBUG 取出助教老师名称
			
			//debug  取出课程 的年级编号
			$page_array[$key]['grade_id'] = DB::result_first("SELECT grade_id FROM ".DB::table("vod")." WHERE id='".$value['v_id']."' LIMIT 1");
		}
		//总页数
		$realpages = @ceil($pagetotal / $numPerPage);
		if($realpages <= 0){
			$realpages=1;
		}
		//前一页
		$page_pre = max(1, intval(($pageNum-1)));
		//后一页
		$page_next = min(intval(($pageNum+1)), $realpages);
		//DEBUG 获取课程类型数组
		include template('admin/hschool/vod_hour');
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error='true',$message="操作失败",$data=array()) {
		$return_array = array(
			"error" => $error,
			"message" => $message,
			"data" => $data
		);
		echo json_ext($return_array);
		die;
	}
}
?>